package welsir.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import welsir.entity.dos.*;
import welsir.entity.vo.ArticlePageVO;
import welsir.entity.vo.ArticleVO;
import welsir.entity.vo.SeedingPageVO;
import welsir.entity.vo.SeedingVO;
import welsir.mapper.DiseaseMapper;
import welsir.mapper.SeedingDiseaseMapper;
import welsir.mapper.SeedingMapper;
import welsir.service.ISeedingService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author welsir
 * @date 2023/3/8 0:35
 */
@Service
@Slf4j
public class ISeedingServiceImpl extends ServiceImpl<SeedingMapper, Seeding> implements ISeedingService {

    @Resource
    private SeedingMapper seedingMapper;
    @Resource
    private DiseaseMapper diseaseMapper;

    @Resource
    private SeedingDiseaseMapper seedingDiseaseMapper;

    @Override
    public boolean updateById(Seeding seeding) {
        return seedingMapper.updateById(seeding) == 1;
    }

    @Override
    public int insert(Seeding seeding) {
        return seedingMapper.insert(seeding);
    }

    public SeedingVO insertDisease(Seeding seeding){
        SeedingVO seedingVO = new SeedingVO();
        List<SeedingDisease> seedingDiseaseList = seedingDiseaseMapper.selectList(new QueryWrapper<SeedingDisease>().eq("seeding_id", seeding.getId()));
        ArrayList<Long> diseaseIds = new ArrayList<>();
        seedingDiseaseList.stream().forEach(seedingDisease -> {
            diseaseIds.add(seedingDisease.getDiseaseId());
        });
        List<Disease> disease = new ArrayList<>();
        if(diseaseIds.size() != 0){
            disease = diseaseMapper.selectList(new QueryWrapper<Disease>().in("id", diseaseIds));
        }
        BeanUtils.copyProperties(seeding,seedingVO);
        seedingVO.setDisease(disease);
        return seedingVO;
    }
    @Override
    public SeedingVO queryByName(String name) {

        Seeding seeding = seedingMapper.selectOne(new QueryWrapper<Seeding>().eq("name", name));

        return insertDisease(seeding);
    }

    @Override
    public SeedingVO queryById(Long id) {
        Seeding seeding = seedingMapper.selectById(id);
        return insertDisease(seeding);
    }

    @Override
    public List<SeedingVO> getAllSeeding() {
        List<Seeding> seedings = this.seedingMapper.selectList(null);
        ArrayList<SeedingVO> seedingVOS = new ArrayList<>();
//        for (Seeding seeding : seedings) {
//            Disease disease = diseaseMapper.selectById(seeding.getDiseaseId());
//            seedingVOS.add(SeedingVO.change(seeding,disease));
//        }
        return seedingVOS;
    }

    @Override
    public Integer deleteById(Long id) {
        return this.seedingMapper.deleteById(id);
    }
    @Override
    public SeedingPageVO VoList(Integer page, Integer limit) {
        List<SeedingVO> result = new ArrayList<>();

        Page<Seeding> seedingPage = new Page<>(page,limit);

        QueryWrapper<Seeding> wrapper= new QueryWrapper<Seeding>();
        wrapper.orderByDesc("id");

        Page<Seeding> resultPage = this.seedingMapper.selectPage(seedingPage, wrapper);


        List<Seeding> seedings = resultPage.getRecords();

       for (Seeding seeding : seedings) {
            SeedingVO seedingVO = insertDisease(seeding);
            result.add(seedingVO);
        }
        SeedingPageVO seedingPageVO = new SeedingPageVO();
        seedingPageVO.setData(result);
        seedingPageVO.setTotal(resultPage.getTotal());
        seedingPageVO.setPages(resultPage.getPages());
        return seedingPageVO;
    }
}
